<template>
  <section>
    <!-- 工具栏 -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="condition">
        <el-form-item>
          <el-select v-model="condition._area" placeholder="区域编号">
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item>
          <el-select v-model="condition._dorm" placeholder="宿舍楼编号">
            <el-option
              v-for="item in dorm"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item>
          <el-input v-model="condition.goodsNumber" placeholder="物品编码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="condition.goodsName" placeholder="物品名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 操作栏 -->
    <el-col>
      <el-form :inline="true">
        <el-form-item>
          <el-button size="small" type="success" @click="newFormVisible = true">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="success" @click="outExcel">导出EXCEL</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 数据展示 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="selsChange"
      v-loading="Loading"
    >
      <el-table-column align="center" type="selection" fixed width="50"></el-table-column>
      <el-table-column align="center" label="序号" fixed type="index" width="50"></el-table-column>
      <el-table-column align="center" label="区域名称" prop="affiliationName" fixed min-width="150"></el-table-column>
      <el-table-column align="center" label="物品名称" prop="goodsName" min-width="100"></el-table-column>
      <el-table-column align="center" label="资产编码" prop="goodsNumber" min-width="100"></el-table-column>
      <el-table-column align="center" label="单价" prop="unitPrice" min-width="60"></el-table-column>
      <el-table-column align="center" label="赔偿价格" prop="compensationPrice" min-width="60"></el-table-column>
      <el-table-column align="center" label="总数量" prop="count" min-width="50"></el-table-column>
      <el-table-column align="center" label="可用数量" prop="usable" min-width="50"></el-table-column>
      <el-table-column align="center" label="已领用数量" prop="used" min-width="50"></el-table-column>
      <el-table-column align="center" label="未归还数量" prop="unReturn" min-width="50"></el-table-column>
      <el-table-column align="center" label="备注" prop="remark" min-width="100"></el-table-column>
      <el-table-column align="center" label="操作" min-width="150" fixed="right">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row)">停用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="currentChange"
        :current-page="currentPage"
        :total="totalPage"
        style="float:right;"
      ></el-pagination>
    </el-col>

    <!-- 新增 -->
    <el-dialog title="新增" :visible.sync="newFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="newForm" :model="newGoods" :rules="rule">
        <el-form-item label="所属区域" prop="_area">
          <el-select v-model="newGoods._area" placeholder="区域编号">
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用品名称" prop="goodsName">
          <el-input v-model="newGoods.goodsName" placeholder="用品名称"></el-input>
        </el-form-item>
        <el-form-item label="资产编码" prop="goodsNumber">
          <el-input
            v-model="newGoods.goodsNumber"
            placeholder="资产编码"
            @blur="checkGoodsName(newGoods.goodsNumber,'new')"
          ></el-input>
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input v-model="newGoods.unitPrice" placeholder="单价"></el-input>
        </el-form-item>
        <el-form-item label="赔偿价格" prop="compensationPrice">
          <el-input v-model="newGoods.compensationPrice" placeholder="赔偿价格"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="count">
          <el-input v-model="newGoods.count" placeholder="数量"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="newGoods.remark" placeholder="备注" type="textarea" autosize></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="newFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="newSubmit('newForm')" :loading="newLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 变更 -->
    <el-dialog title="编辑" :visible.sync="alterFormVisible" :close-on-click-modal="false">
      <el-form label-width="6rem" ref="alterForm" :model="alterGoods" :rules="rule">
        <el-form-item label="所属区域">
          <el-input v-model="alterGoods.affiliationName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="用品名称" prop="goodsName">
          <el-input v-model="alterGoods.goodsName" placeholder="用品名称"></el-input>
        </el-form-item>
        <el-form-item label="资产编码" prop="goodsNumber">
          <el-input v-model="alterGoods.goodsNumber" placeholder="资产编码" disabled></el-input>
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input v-model="alterGoods.unitPrice" placeholder="单价"></el-input>
        </el-form-item>
        <el-form-item label="赔偿价格" prop="compensationPrice">
          <el-input v-model="alterGoods.compensationPrice" placeholder="赔偿价格"></el-input>
        </el-form-item>
        <el-form-item label="总数量" prop="count">
          <el-input v-model="alterGoods.count" placeholder="总数量"></el-input>
        </el-form-item>
        <el-form-item label="未归还数量" prop="unReturn">
          <el-input v-model="alterGoods.unReturn" placeholder="未归还数量"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="alterGoods.remark" placeholder="备注" type="textarea" autosize></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="alterFormVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click.native="alterSubmit('alterForm')"
          :loading="alterLoading"
        >提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { getArea } from "../../api/area.js";
import {
  searchUseGoods,
  newUseGoods,
  checkNumberRepeat,
  altertUseGoods,
  deleteUseGoods
} from "../../api/goods.js";
export default {
  data() {
    // 进行数字验证
    var checkNumber = (rule, value, callback) => {
      var re = /^[0-9]+.?[0-9]*$/;
      if (!value) {
        return callback(new Error("值不能为空"));
      }
      setTimeout(() => {
        if (!re.test(value)) {
          callback(new Error("请输入数字值"));
        } else {
          callback();
        }
      }, 100);
    };
    return {
      condition: {
        _area: null,
        goodsNumber: null,
        goodsName: null,
        page: 1
      },
      tableData: [
        {
          _area: null,
          affiliationName: null,
          goodsName: null,
          goodsNumber: null,
          unitPrice: null,
          compensationPrice: null,
          count: null,
          remark: null
        }
      ],
      newGoods: {
        _area: null,
        goodsName: null,
        goodsNumber: null,
        unitPrice: null,
        compensationPrice: null,
        count: null,
        remark: null
      },
      rule: {
        _area: [
          { required: true, message: "请选择宿舍楼区域", trigger: "change" }
        ],
        goodsName: [
          { required: true, message: "请输入物品名称", trigger: "blur" }
        ],
        goodsNumber: [
          { required: true, message: "请输入物品编码", trigger: "blur" }
        ],
        unitPrice: [
          {
            validator: checkNumber,
            required: true,
            trigger: "blur"
          }
        ],
        compensationPrice: [
          {
            validator: checkNumber,
            required: true,
            trigger: "blur"
          }
        ],
        count: [
          {
            validator: checkNumber,
            required: true,
            trigger: "blur"
          }
        ],
        unReturn: [
          {
            validator: checkNumber,
            required: true,
            trigger: "blur"
          }
        ],
        remark: [{ required: false }]
      },
      alterGoods: {},
      area: [
        {
          value: "",
          label: ""
        }
      ],
      // dorm: [
      //   {
      //     value: "",
      //     label: ""
      //   }
      // ],
      newFormVisible: false,
      alterFormVisible: false,
      Loading: false,
      newLoading: false,
      alterLoading: false,
      currentPage: 1,
      totalPage: 1
    };
  },
  methods: {
    getArea() {
      getArea().then(res => {
        if (res) {
          this.area = res.msg;
        }
      });
    },
    // areaChange() {},
    //查询
    getData() {
      this.Loading = true;
      searchUseGoods(this.condition).then(res => {
        this.Loading = false;
        if (res) {
          this.totalPage = res.page;
          this.tableData = res.msg;
        } else {
          this.tableData = null;
          this.totalPage = 0;
          this.currentPage = 0;
        }
      });
    },
    search() {
      this.condition.page = 1;
      this.getData();
    },
    // 分页
    currentChange(val) {
      this.condition.page = val === undefined ? this.currentPage : val;
      this.getData();
    },
    // 新增
    newSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 数量不得为负
          if (this.newGoods.count < 0) {
            this.$message.error("数量不得未负");
            return false;
          }
          this.newLoading = true;
          newUseGoods(this.newGoods).then(res => {
            this.newLoading = false;
            if (res) {
              this.$message.success(res.msg);
              this.$refs[formName].resetFields();
              this.newFormVisible = false;
              this.getData();
            }
          });
        } else {
          this.$message.error("请将信息填写完整!");
          return false;
        }
      });
    },
    outExcel() {
      // 导出明细
      window.location.href =
        "http://192.168.10.225:80/dormitoryA/Property/Property/receivedGoodsExcelOut";
    },
    // 点击编辑:
    handleEdit(index, row) {
      this.alterFormVisible = true;
      this.alterGoods = Object.assign({}, row);
    },
    // 修改
    alterSubmit(forName) {
      this.$refs[forName].validate(valid => {
        if (valid) {
          this.alterLoading = true;
          altertUseGoods(this.alterGoods).then(res => {
            this.alterLoading = false;
            if (res) {
              this.$message.success(res.msg);
            }
            this.getData();
            this.alterFormVisible = false;
            return true;
          });
        } else {
          this.$message.error("请将信息填写完整!");
          return false;
        }
      });
    },
    // 点击删除
    handleDel(row) {
      this.$confirm("此操作将永久停用该物品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteUseGoods(row.serial).then(res => {
            if (res) {
              this.$message({
                type: "success",
                message: "停用成功!"
              });
              this.getData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    selsChange: function(sels) {
      this.sels = sels;
    },
    checkGoodsName(goodsNumber, type) {
      this.Loading = true;
      checkNumberRepeat(goodsNumber, "use").then(res => {
        this.Loading = false;
        if (!res) {
          // this.$message.error("编码重复!");
          if (type === "new") {
            this.newGoods.goodsNumber = "";
          } else if (type === "alter") {
            this.alterGoods.goodsNumber = "";
          }
          return false;
        } else {
          // this.initializeLoading = false;
          return true;
        }
      });
    }
  },
  mounted() {
    this.getArea();
    this.getData();
  }
};
</script>
<style lang="">
</style>